<?php
class ControllerAffiliateTracking extends FrontController {
	public function index() {
		if (!$this->affiliate->isLogged()) {
	  		$this->session->data['redirect'] = US('affiliate/tracking');
	  		$this->redirect(US('affiliate/login'));
    	}

		$this->document->setTitle($this->_['heading_title']);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['text_account'],
			'href'      => US('affiliate/account'),
        	'separator' => $this->_['text_separator']
      	);

      	$this->data['breadcrumbs'][] = array(
        	'text'      => $this->_['heading_title'],
			'href'      => US('affiliate/tracking'),
        	'separator' => $this->_['text_separator']
      	);

		$this->data['text_description'] = sprintf($this->_['text_description'], C('config_name'));

    	$this->data['code'] = $this->affiliate->getCode();

    	$this->display('affiliate/tracking.tpl');
  	}

	public function autocomplete() {
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			M('catalog/product');

			$data = array(
				'filter_name' => $this->request->get['filter_name'],
				'start'       => 0,
				'limit'       => 20
			);

			$results = $this->model_catalog_product->getProducts($data);

			foreach ($results as $result) {
				$json[] = array(
					'name' => strip_tags(html_entity_decode($result['name'], ENT_QUOTES, 'UTF-8')),
					'link' => str_replace('&amp;', '&', U('product/product', 'product_id=' . $result['product_id'] . '&tracking=' . $this->affiliate->getCode()))
				);
			}
		}

		$this->response->setOutput(json_encode($json));
	}
}
?>